package com.ruoyi.web.controller.repair;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.DataUtils;
import com.ruoyi.data.MD5;
import com.ruoyi.dataMigration.domain.ProjectDataT;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.repairQuery.domain.RepairQueryT;
import com.ruoyi.repairQuery.service.IRepairQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * 维修记录查询Controller
 * 
 * @author ruoyi
 * @date 2023-05-31
 */
@RestController
@RequestMapping("/repair/repairQuery")
public class RepairQueryController extends BaseController
{
    @Autowired
    private IRepairQueryService repairQueryService;

    /**
     * 查询维修记录查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RepairQueryT repairQuery) throws Exception {
        startPage();
        RepairQueryT repairQueryVO = new RepairQueryT();
        List<RepairQueryT> list = repairQueryService.selectRepairQueryList(repairQuery);
        if (list.size()!=0) {
            for (RepairQueryT repairVO : list) {
                repairVO.setPapersCode(MD5.decrypt(repairVO.getPapersCode()));
                repairVO.setUserName(MD5.decrypt(repairVO.getUserName()));
            }
            if (getDataTableSumJudge(list)) {
                repairQueryVO = repairQueryService.selectRepairQuerySum(repairQuery);
                repairQueryVO.setProjectId("合计");
                list.add(repairQueryVO);
            }
        }
        return getDataTable(list);
    }
    /**
     * 查询维修记录查询列表
     */
    @GetMapping("/list1")
    public TableDataInfo list1(RepairQueryT repairQuery) throws Exception {
        startPage();
        RepairQueryT repairQueryVO = new RepairQueryT();
        List<RepairQueryT> list = repairQueryService.selectRepairQueryList1(repairQuery);
        if (list.size()!=0) {
            for (RepairQueryT repairVO : list) {
                repairVO.setPapersCode(MD5.decrypt(repairVO.getPapersCode()));
                repairVO.setUserName(MD5.decrypt(repairVO.getUserName()));
            }
            if (getDataTableSumJudge(list)) {
                repairQueryVO = repairQueryService.selectRepairQuerySum1(repairQuery);
                repairQueryVO.setProjectId("合计");
                list.add(repairQueryVO);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出维修记录查询列表
     */
    @Log(title = "维修记录查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RepairQueryT repairQuery) throws Exception {
        RepairQueryT repairQueryVO = new RepairQueryT();
        List<RepairQueryT> list = repairQueryService.selectRepairQueryList(repairQuery);
        for (RepairQueryT repairVO : list) {
            repairVO.setPapersCode(MD5.decrypt(repairVO.getPapersCode()));
            repairVO.setUserName(MD5.decrypt(repairVO.getUserName()));
        }
        if(getDataTableSumJudge(list)){
            repairQueryVO = repairQueryService.selectRepairQuerySum(repairQuery);
            repairQueryVO.setProjectId("合计");
            list.add(repairQueryVO);
        }
        ExcelUtil<RepairQueryT> util = new ExcelUtil<RepairQueryT>(RepairQueryT.class);
        util.exportExcel(response, list, "维修记录查询数据");
    }

    /**
     * 获取维修记录查询详细信息
     */
    @GetMapping(value = "/{checkId}")
    public AjaxResult getInfo(@PathVariable("checkId") Long checkId)
    {
        return AjaxResult.success(repairQueryService.selectRepairQueryByCheckId(checkId));
    }

    @GetMapping("/updateBalnce")
    public AjaxResult updateBalnce()
    {
        List<RepairQueryT> doorList=repairQueryService.selectDoorList();
        for (RepairQueryT doorvo:doorList) {
            RepairQueryT hpt = new RepairQueryT();
            hpt.setProjectSts("1");
            hpt.setHouseId(doorvo.getHouseId());
            List<RepairQueryT> list = repairQueryService.selectProject(hpt);
            if(list!=null) {
                for (RepairQueryT rq : list) {
                    RepairQueryT costMoneySum=repairQueryService.selectCostMoneySum(rq);
                    if (costMoneySum==null){
                        continue;
                    }
                    RepairQueryT repairMoneySum=repairQueryService.selectRepairMoneySum(rq);
                    RepairQueryT updateRepair = new RepairQueryT();
                    updateRepair.setHouseId(rq.getHouseId());
                    updateRepair.setProjectId(rq.getProjectId());
                    Double balance=0d;
                    balance=DataUtils.subMoney(costMoneySum.getCostMoney(),repairMoneySum.getPayBalance());
                    if(balance==0){
                        balance=rq.getPayBalance();
                    }
                    updateRepair.setBalance(balance);
                    Double balanceRatio = DataUtils.divideMoney(balance, doorvo.getBudgetMoney()) * 100;
                    updateRepair.setBalanceRatio(balanceRatio);
                    Double repairRatio = DataUtils.divideMoney(rq.getPayBalance(), balance) * 100;
                    updateRepair.setRepairRatio(repairRatio);
                    repairQueryService.updateRepairBalance(updateRepair);
                }
            }
        }

        return AjaxResult.success(1);
    }

}
